package com.fire.web.modules.all.controller;

import com.fire.web.modules.all.constant.ErrorCodeConstants;
import com.fire.web.modules.all.entity.AjaxResult;
import com.fire.web.modules.all.entity.FireProprietorModel;
import com.fire.web.modules.all.entity.SiteModel;
import com.fire.web.modules.all.request.AddReportBuildingZone;
import com.fire.web.modules.all.request.AddReportInfoDetail;
import com.fire.web.modules.all.service.IBuildingZoneService;
import com.fire.web.modules.all.entity.BuildingZoneModel;
import com.fire.web.modules.all.service.IFireProprietorService;
import com.fire.web.modules.all.service.ISequenceService;
import com.fire.web.modules.all.service.ISiteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 信息填报控制器
 *
 * @author Matthew
 * @Date 2019-02-23 16:37
 */
@RestController
@RequestMapping("/reportinfo")
public class ReportInfoController extends CrudController<IBuildingZoneService, BuildingZoneModel> {

	private static final Logger LOGGER = LoggerFactory.getLogger(ReportInfoController.class);

	@Autowired
	private ISiteService siteService;
	@Autowired
	private IBuildingZoneService buildingZoneService;
	@Autowired
	private IFireProprietorService proprietorService;
	@Autowired
	private ISequenceService sequenceService;

	/**
	 * 查询填报信息详情
	 *
	 * @return
	 */
	@PostMapping("/detail/{siteId}")
	@ResponseBody
	public AjaxResult detail(@PathVariable("siteId") String siteId) {
		LOGGER.info("==================查询填报信息详情开始==================");
		AjaxResult ajaxResult;
		Map result = new HashMap();
		try {
			SiteModel site = new SiteModel();
			site.setSiteid(siteId);
			site = siteService.selectOne(site);
			if(site==null){
				LOGGER.error("无对应站点信息");
				return new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误，无对应站点信息", "系统内部错误，无对应站点信息");
			}
			String proprietor = site.getProprietor();
			String buildingId = site.getBuildingid();
			// 查询建筑编码表信息
			BuildingZoneModel buildingZone = new BuildingZoneModel();
			buildingZone.setBuildingid(buildingId);
			List<BuildingZoneModel> buildingZoneList = buildingZoneService.selectList(buildingZone);
			if(buildingZoneList.size()==0){
				LOGGER.info("无建筑编码表信息，新建对应信息");
				// 新增地理编码表
				buildingZone = new BuildingZoneModel();
				String zoneId = sequenceService.getNextStringValue("zoneid");
				buildingZone.setZoneid(zoneId);
				buildingZone.setCreatetime(new Date());
				buildingZone.setBuildingid(buildingId);
				buildingZoneService.insert(buildingZone);
			}else{
				LOGGER.info("查询到建筑编码表对应信息");
				buildingZone = buildingZoneList.get(0);
			}
			result.put("buildReportInfo",buildingZone);

			// 消防单位表
			FireProprietorModel fireProprietor = new FireProprietorModel();
			fireProprietor.setProprietor(proprietor);
			fireProprietor = proprietorService.selectOne(fireProprietor);
			if(fireProprietor==null){
				LOGGER.error("无对应消防单位信息");
				ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误，无对应消防单位信息", "系统内部错误，无对应消防单位信息");
			}
			result.put("proprietorReportInfo",fireProprietor);

			ajaxResult = new AjaxResult(ErrorCodeConstants.SUCCESS,"查询成功",result,"查询成功");
		} catch (Exception e) {
			LOGGER.error("ReportInfoController.detail().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);
		LOGGER.info("==================查询填报信息详情结束==================");

		return ajaxResult;
	}

	/**
	 * 信息填报
	 *
	 * @return
	 */
	@PostMapping("/updateReportInfoDetail")
	@ResponseBody
	public AjaxResult updateReportInfoDetail(AddReportInfoDetail req) {
		LOGGER.info("==================信息填报开始==================");
		AjaxResult ajaxResult;
		try {
			buildingZoneService.updateReportInfoAllCoumnById(req);
//			siteService.updateSiteInSmartThing(req);
			ajaxResult = new AjaxResult(ErrorCodeConstants.SUCCESS, "填报成功", "填报成功");
		} catch (Exception e) {
			LOGGER.error("ReportInfoController.updateReportInfoDetail().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);
		LOGGER.info("==================信息填报结束==================");

		return ajaxResult;
	}

}